package com.ffyc.resale.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.model.Forum;
import com.ffyc.resale.model.ForumComment;
import com.ffyc.resale.model.Result;
import com.ffyc.resale.model.User;
import com.ffyc.resale.service.ForumService;
import org.springframework.beans.Mergeable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.List;

// 帖子控制层
@RestController
@RequestMapping("api/forum")
public class ForumController {
    @Autowired
    private ForumService forumService;

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        forumService.deleteById(id);
        return new Result(200, "成功", null);
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        forumService.deleteBatch(ids);
        return new Result(200, "成功", null);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Forum forum) {
        forumService.updateById(forum);
        return new Result(200, "成功", null);
    }


    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Forum forum,
                             @RequestParam(defaultValue = "1") Integer pageNo,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Forum> page = forumService.selectPage(forum, pageNo, pageSize);
        return new Result(200, "成功", page);
    }

    // 帖子列表
    @RequestMapping(value = "forums", method = RequestMethod.GET)
    public Result forums(Forum forum,
                         @RequestParam(defaultValue = "1") Integer pageNo,
                         @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Forum> forumPage = forumService.forums(forum,pageNo,pageSize);
        return new Result(200, "查询成功", forumPage);
    }

    // 首页热门帖子轮播查询
    @RequestMapping(value = "topForums", method = RequestMethod.GET)
    public Result forums(Forum forum) {
        List<Forum> forumList = forumService.TopForums(forum);
        return new Result(200, "查询成功",forumList);
    }

    // 通过帖子id获取用户信息
    @RequestMapping(value = "findUserInfoByForumId",method = RequestMethod.GET)
    public Result userInfo(String forumId){
        User user = forumService.userInfo(forumId);
        return new Result(200,"查询成功",user);
    }

    // 添加帖子
    @RequestMapping(value = "addForum",method = RequestMethod.POST)
    public Result addForum(@RequestBody Forum forum,@RequestHeader("userToken") String userToken){
        forumService.addForum(forum,userToken);
        return new Result(200,"发布成功",null);
    }

    // 通过帖子id查看帖子详情
    @RequestMapping(value = "forumInfo", method = RequestMethod.GET)
    public Result forumInfo(@RequestParam("id") Integer forumId) {
        Forum forum = forumService.findForumInfoById(forumId);
        return new Result(200, "查询成功", forum);
    }
}
